package com.tian.earth.ui.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.annotation.VisibleForTesting
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.tian.earth.R
import com.tian.earth.ui.activity.login.LoginActivity

class BootStrapActivity : BaseActivity() {

    @VisibleForTesting
    lateinit var viewPager: ViewPager
    val images = arrayOf(
        R.mipmap.guide_01,
        R.mipmap.guide_02,
        R.mipmap.guide_03
    )

    var imageViews = ArrayList<ImageView>()

    val adapter = object : PagerAdapter() {
        override fun isViewFromObject(view: View, `object`: Any): Boolean {
            return  view == `object`
        }

        override fun getCount(): Int {
            return  imageViews.size
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            container.removeView(imageViews[position])
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            container.addView(imageViews[position])
            return imageViews[position]
        }

    }



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_welcome)
        initImageViews()
        viewPager = findViewById(R.id.vp_welcome)
        viewPager.adapter = adapter
    }

    private fun initImageViews() {
        imageViews.clear()
        var params = ViewPager.LayoutParams()
        for (imageId in images) {
            var imageView = ImageView(this)
            imageView.id = imageId
            imageView.layoutParams = params
            imageView.setImageResource(imageId)
            imageView.scaleType = ImageView.ScaleType.FIT_XY
            imageViews.add(imageView)
            if (imageId == R.mipmap.guide_03) {
                imageView.setOnClickListener {
                    startActivity(Intent(this, LoginActivity::class.java))
                }
            }
        }
    }


}
